package com.zhangxin.threadcoreknowledge.threadobjectclasscommonmethods;

import org.omg.CORBA.INITIALIZE;

/**
 * 通过wait 和 notify方法来实现
 */
public class Demo06WaitNotifyPrintOddEvenWait {

    private static int count = 0;

    private static final Object lock = new Object();

    public static void main(String[] args) {
        new Thread(new TuriningRunner(), "odd").start();
        new Thread(new TuriningRunner(), "even").start();
    }


    // 1.拿到锁就就打印
    // 2.打印完,唤醒其他线程,并休眠自己
    static class TuriningRunner implements Runnable{
        @Override
        public void run() {
            while (count < 100) {
                synchronized (lock) {
                    System.out.println(Thread.currentThread().getName() + ": "+ count++);
                    lock.notify();
                    if (count < 100) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }


        }
    }

}
